<?php

/**
 * @copyright   FOSP
 * @version     20120409
 * @todo        Match Extend model
 * @name        MatchExtend
 * @author      TrongNV 
 */
class MatchExtend {
    const MATCH_EXTEND              = 'match_extend';
    const PREDICT_MATCH_ALL         = 'predict_match_all';
    const PREDICT_MATCH_TOP         = 'predict_match_top';
    const PREDICT_MATCH_TYPE        = 'predict_match_type';
    const PREDICT_MATCH_TOTAL       = 'predict_match_total';
    const COMMENT_LIST              = 'comment_list';

    /**
     *
     * @var MatchExtend
     */
    protected static $instance;

    protected function __construct() {
        
    }

    /**
     * Get MatchExtend's instance
     * @return MatchExtend
     * @author TrongNV
     */
    public static function getInstance() {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    /**
     * Get match extend by match
     * @param int $matchID
     * @return array|boolean 
     * @author TrongNV
     */
    public function getDetailMatchExtend($intMatchID) {
        $memcacheInstance = Fpt_Global::getMemcacheInstance($intMatchID);
        $keyCache = Fpt_Global::makeCacheKey(self::MATCH_EXTEND, $intMatchID);

        // Read cache
        $arrMatchExtend = $memcacheInstance->read($keyCache);
        
        // Miss cache
        if ($arrMatchExtend === FALSE) {
            try {
                $config = Fpt_Global::getApplicationIni();
                // Get db matchExtend instance
                $matchExtendObj = Fpt_Business_MatchExtend::factory($config['database']['default']);

                // Get data from db
                $arrMatchExtend = $matchExtendObj->getDetailMatchExtendByIDs($intMatchID);
                
                // Write to cache
                if (!empty($arrMatchExtend))
                {
                    Fpt_Global::writeMemcache($intMatchID, $keyCache, $arrMatchExtend);
                }
                
            } catch (Exception $ex) {
                Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
                return FALSE;
            }
        }
        return $arrMatchExtend;
    }

    /**
     * Insert Match predict
     * @author TrongNV
     */
    public function insertMatchPredict($params) {
        $result = array();
        $config = Fpt_Global::getApplicationIni();
        $codePredict = Fpt_Utility::randomWord(6);

        // Get db matchExtend instance
        $matchExtendObj = Fpt_Business_MatchExtend::factory($config['database']['default']);
        $params['code_predict'] = $codePredict;
        
        // Get data from db
        $result['result'] = $matchExtendObj->insertMatchPredict($params);
        
        // Delete cache
        if ($result['result']) {
            $memcacheInstance = Fpt_Global::getMemcacheInstance($params['match_id']);
            $keyCacheMaster   = Fpt_Global::makeCacheKey(self::PREDICT_MATCH_ALL, $params['match_id']);

            // Delete all cache from master cache
            $arrCacheMaster = $memcacheInstance->read($keyCacheMaster);
            if (!empty($arrCacheMaster)) {
                foreach ($arrCacheMaster as $keyCache) {
                    Fpt_Global::deleteMemcache($params['match_id'], $keyCache);
                }
                Fpt_Global::deleteMemcache($params['match_id'], $keyCacheMaster);
            }
        }
        
        $result['code_predict'] = $codePredict;
        return $result;
    }

    /**
     * Insert Comment match extend
     * @author TrongNV
     */
    public function insertCommentMatchExtend($params) {
        $config = Fpt_Global::getApplicationIni();
        // Get db matchExtend instance
        $matchExtendObj = Fpt_Business_MatchExtend::factory($config['database']['default']);

        // Get data from db
        $result = $matchExtendObj->insertCommentMatchExtend($params);
        
         // Delete cache
        /*if (!empty($result)) {
            $keyCache   = Fpt_Global::makeCacheKey(self::COMMENT_LIST, array($params['match_id'], $params['team_id']));
            Fpt_Global::deleteMemcache($params['match_id'], $keyCache);
        }*/
        return $result;
    }

    /**
     * Get Comment match extend by match_id and team_id
     * @author TrongNV
     */
    public function getCommentByMatch($intMatchID, $intTeamID, $strIsApproved) {
        $memcacheInstance = Fpt_Global::getMemcacheInstance($intMatchID);
        $keyCache         = Fpt_Global::makeCacheKey(self::COMMENT_LIST, array($intMatchID, $intTeamID));
        
        //Read cache
        $arrCommentMatchExtend = $memcacheInstance->read($keyCache); 
        
        if ($arrCommentMatchExtend == FALSE) {
            try {
                $config = Fpt_Global::getApplicationIni();
                // Get db matchExtend instance
                $matchExtendObj = Fpt_Business_MatchExtend::factory($config['database']['default']);

                // Get comment match extend from db
                $arrCommentMatchExtend = $matchExtendObj->getCommentByMatch($intMatchID, $intTeamID, $strIsApproved);

                // Write to cache
                if (!empty($arrCommentMatchExtend)){
                    Fpt_Global::writeMemcache($intMatchID, $keyCache, $arrCommentMatchExtend);
                }
            } catch (Exception $ex) {
                Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
                return FALSE;
            }
        }
        return $arrCommentMatchExtend;
    }

    /**
     * Get report match predict by match_id
     * @author TrongNV
     */
    public function getReportMatchPredict($intMatchID, $intType) {
        $memcacheInstance = Fpt_Global::getMemcacheInstance($intMatchID);
        $keyCache         = Fpt_Global::makeCacheKey(self::PREDICT_MATCH_TYPE, array($intMatchID, $intType));
        $keyCacheMaster   = Fpt_Global::makeCacheKey(self::PREDICT_MATCH_ALL, $intMatchID);
        //Read cache
        $arrReportMatchPredict = $memcacheInstance->read($keyCache);        
        
        //Miss cache
        if ($arrReportMatchPredict === FALSE) {
            try {
                $config = Fpt_Global::getApplicationIni();
                // Get db matchExtend instance
                $matchExtendObj = Fpt_Business_MatchExtend::factory($config['database']['default']);

                // Get comment match extend from db
                $arrReportMatchPredict = $matchExtendObj->getReportMatchPredict($intMatchID, $intType);
                
                // Write to cache
                if (!empty($arrReportMatchPredict))
                {
                    Fpt_Global::writeMemcache($intMatchID, $keyCache, $arrReportMatchPredict);
                    
                    // Write master cache
                    $arrCacheMaster = $memcacheInstance->read($keyCacheMaster);
                    if (empty($arrCacheMaster)) {
                        $arrCacheMaster = array();
                    }
                    $arrCacheMaster = array_merge($arrCacheMaster, array($keyCache=>$keyCache));
                    Fpt_Global::writeMemcache($intMatchID, $keyCacheMaster, $arrCacheMaster);
                }
                
                
            } catch (Exception $ex) {
                Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
                return FALSE;
            }
        }
        return $arrReportMatchPredict;
    }

    /**
     * Get total match predict by match_id
     * @author TrongNV
     */
    public function getTotalMatchPredict($intMatchID) {
        $memcacheInstance = Fpt_Global::getMemcacheInstance($intMatchID);
        $keyCache         = Fpt_Global::makeCacheKey(self::PREDICT_MATCH_TOTAL, $intMatchID);
        $keyCacheMaster   = Fpt_Global::makeCacheKey(self::PREDICT_MATCH_ALL, $intMatchID);

        //Read cache
        $arrTotalMatchPredict = $memcacheInstance->read($keyCache);
        
        //Miss cache
        if ($arrTotalMatchPredict === FALSE) {
            try {
                $config = Fpt_Global::getApplicationIni();
                // Get db matchExtend instance
                $matchExtendObj = Fpt_Business_MatchExtend::factory($config['database']['default']);

                // Get comment match extend from db
                $arrTotalMatchPredict = $matchExtendObj->getTotalMatchPredict($intMatchID);
                
                // Write to cache
                if (!empty($arrTotalMatchPredict))
                {
                    Fpt_Global::writeMemcache($intMatchID, $keyCache, $arrTotalMatchPredict);
                    
                    // Write master cache
                    $arrCacheMaster = $memcacheInstance->read($keyCacheMaster);
                    if (empty($arrCacheMaster)) {
                        $arrCacheMaster = array();
                    }
                    $arrCacheMaster = array_merge($arrCacheMaster, array($keyCache=>$keyCache));
                    Fpt_Global::writeMemcache($intMatchID, $keyCacheMaster, $arrCacheMaster);
                }
                
                
            } catch (Exception $ex) {
                Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
                return FALSE;
            }
        }
        return $arrTotalMatchPredict;
    }

    /**
     * Get top match predict by match_id
     * @author TrongNV
     */
    public function getTopMatchPredict($intMatchID, $limit) {
        $memcacheInstance   = Fpt_Global::getMemcacheInstance($intMatchID);
        $keyCache           = Fpt_Global::makeCacheKey(self::PREDICT_MATCH_TOP, array($intMatchID, $limit));
        $keyCacheMaster     = Fpt_Global::makeCacheKey(self::PREDICT_MATCH_ALL, $intMatchID);
        
        //Read cache
        $arrTopMatchPredict = $memcacheInstance->read($keyCache);
        
        //Miss cache
        if ($arrTopMatchPredict === FALSE) {
            try {
                $config = Fpt_Global::getApplicationIni();
                // Get db matchExtend instance
                $matchExtendObj = Fpt_Business_MatchExtend::factory($config['database']['default']);

                // Get comment match extend from db
                $arrTopMatchPredict = $matchExtendObj->getTopMatchPredict($intMatchID, $limit);
                
                // Write to cache
                if (!empty($arrTopMatchPredict))
                {
                    Fpt_Global::writeMemcache($intMatchID, $keyCache, $arrTopMatchPredict);
                    
                    // Write master cache
                    $arrCacheMaster = $memcacheInstance->read($keyCacheMaster);
                    if (empty($arrCacheMaster)) {
                        $arrCacheMaster = array();
                    }
                    $arrCacheMaster = array_merge($arrCacheMaster, array($keyCache=>$keyCache));
                    Fpt_Global::writeMemcache($intMatchID, $keyCacheMaster, $arrCacheMaster);
                }
            } catch (Exception $ex) {
                Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
                return FALSE;
            }
        }
        return $arrTopMatchPredict;
    }

    /**
     * Get detail match extend by code  
     */
    public function getDetailMatchExtendByCode($strMatchCode) {
        try {
            $config = Fpt_Global::getApplicationIni();
            // Get db matchExtend instance
            $matchExtendObj = Fpt_Business_MatchExtend::factory($config['database']['default']);

            // Get comment match extend from db
            $arrMatchExtendByCode = $matchExtendObj->getDetailMatchExtendByCode($strMatchCode);
                
         } catch (Exception $ex) {
             Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
             return FALSE;
         }
        return $arrMatchExtendByCode;
    }

    /**
     * Update predict match when user use SMS => update field send_sms ( 1 )
     */
    public function updateMatchPredict($intMatchID, $strMobile, $strCodePredict) {
        try {

            $config = Fpt_Global::getApplicationIni();
            // Get db matchExtend instance
            $matchExtendObj = Fpt_Business_MatchExtend::factory($config['database']['default']);

            // Get comment match extend from db
            $result = $matchExtendObj->updateMatchPredict($intMatchID, $strMobile, $strCodePredict);
            
            // Delete cache
            if ($result) {
                $memcacheInstance = Fpt_Global::getMemcacheInstance($intMatchID);
                $keyCacheMaster   = Fpt_Global::makeCacheKey(self::PREDICT_MATCH_ALL, $intMatchID);

                // Delete all cache from master cache
                $arrCacheMaster = $memcacheInstance->read($keyCacheMaster);
                if (!empty($arrCacheMaster)) {
                    foreach ($arrCacheMaster as $keyCache) {
                        Fpt_Global::deleteMemcache($intMatchID, $keyCache);
                    }
                    Fpt_Global::deleteMemcache($intMatchID, $keyCacheMaster);
                }
            }
            
        } catch (Exception $ex) {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
        return $result;
    }

    /**
     * Insert Sms predict 
     */
    public function insertSmsPredict($params) {

        $config = Fpt_Global::getApplicationIni();
        // Get db matchExtend instance
        $matchExtendObj = Fpt_Business_MatchExtend::factory($config['database']['default']);

        // Get data from db
        $result = $matchExtendObj->insertSmsPredict($params);
        return $result;
    }

}

?>
